package org.ling.xuan.entity;


import cn.hutool.core.util.StrUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class HundSunVersionCheckFile {
    private final String contentStr;

    public HundSunVersionCheckFile(String contentStr) {
        this.contentStr = contentStr;

    }


    /**
     * 是否注释开始
     *
     * @return {@link String}
     */
    public boolean isAnnotationStart() {
        String fileContent = contentStr;
        String regex = "^\\s*/\\*.*?\\*/";
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(fileContent);
        return matcher.find();
    }

    /**
     * 获取注释范围内容
     *
     * @return {@link String}
     */
    public String getAnnotationRangeContent() {
        String fileContent = contentStr;
        if (isAnnotationStart()) {
            String regex = "^\\s*/\\*.*?\\*/";
            Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
            Matcher matcher = pattern.matcher(fileContent);
            if (matcher.find()) {
                return matcher.group();
            }
        }
        return "";
    }

    /**
     * 获取注释区域的属性名称 如：文件名称，系统版本，开发人员等等
     *
     * @param fieldName 字段名
     * @return {@link String}
     */
    public String getAnnotationField(String fieldName) {
        String content = getAnnotationFieldText(fieldName);
        String regex = "\\s*\\*\\s*" + fieldName + "\\s*:(\\s*.*\\s*)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return matcher.group(1).trim();
        }
        return "";
    }

    /**
     * 获取注释区域的属性的内容 如：文件名称，系统版本，开发人员等等
     *
     * @param fieldName 字段名
     * @return {@link String}
     */
    public String getAnnotationFieldText(String fieldName) {
        String content = getAnnotationRangeContent();
        String regex = "(\\s*\\*\\s*" + fieldName + "\\s*:\\s*.*\\s*)\n";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }


    /**
     * 获取注释区域最后一个版本区域的内容。
     *
     * @return {@link String}
     */
    public String getLastAnnotationVersionContent() {
        String content = getLastAnnotationText();
        Pattern pattern = Pattern.compile("\\s*[\\*]\\s*[Vv]?\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+).*?\n", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }


    /**
     * 是否以修改单号结尾
     *
     * @return boolean
     */
    public String getLastModifyNo() {
        String s = getLastAnnotationText();
        if (StrUtil.isNotBlank(s)) {
            String regex = "[TM]\\d{7,13}";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(s);
            if (matcher.find()) {
                return matcher.group();
            }
        }
        return "";
    }


    /**
     * 判断是否具有HUNDSUN_VERSION属性
     *
     * @return boolean
     */
    public String getHundSunFieldText() {
        String content = contentStr;
        String regex = "(HUNDSUN_VERSION\\s*\\=\\s*\\\".*@version.*\\\"\\s*\\;)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }


    /**
     * 抽取HUNDSUN_VERSION属性中的version
     *
     * @return {@link String}
     */
    public String getHundSunFieldVersion() {
        String content = getHundSunFieldVersionText();
        String versionReg = "@version\\s*+[Vv]?(\\d+\\.\\d+\\.\\d+\\.\\d+)";
        Pattern versionPart = Pattern.compile(versionReg);
        Matcher versionMatcher = versionPart.matcher(content);
        if (versionMatcher.find()) {
            return versionMatcher.group(1);
        }

        return "";
    }

    /**
     * 抽取HUNDSUN_VERSION属性中的version
     *
     * @return {@link String}
     */
    public String getHundSunFieldVersionText() {
        String content = getHundSunFieldText();
        String versionReg = "(@version\\s*+[Vv]?\\d+\\.\\d+\\.\\d+\\.\\d+)";
        Pattern versionPart = Pattern.compile(versionReg);
        Matcher versionMatcher = versionPart.matcher(content);
        if (versionMatcher.find()) {
            return versionMatcher.group(1);
        }

        return "";
    }

    /**
     * 抽取HUNDSUN_VERSION属性中的某注解的属性字符串
     *
     * @return {@link String}
     */
    public String getHundSunFieldAnoText(String annotation) {
        String secondContent = getHundSunFieldText();
        String versionReg = "(@" + annotation + ".*?)[@\"]";
        Pattern versionPart = Pattern.compile(versionReg);
        Matcher versionMatcher = versionPart.matcher(secondContent);
        if (versionMatcher.find()) {
            return versionMatcher.group(1);
        }

        return "";
    }

    /**
     * 抽取HUNDSUN_VERSION属性中的某注解的属性内容
     *
     * @return {@link String}
     */
    public String getHundSunFieldAnoValue(String annotation) {
        String secondContent = getHundSunFieldAnoText(annotation);
        String versionReg = "@" + annotation + "(.*)$";
        Pattern versionPart = Pattern.compile(versionReg);
        Matcher versionMatcher = versionPart.matcher(secondContent);
        if (versionMatcher.find()) {
            return versionMatcher.group(1).trim();
        }

        return "";
    }


    /**
     * 是否有getVersion方法
     *
     * @return boolean
     */
    public String getVersionMethodText() {
        String content = contentStr;
        String regex = "getVersion\\s*\\(\\s*\\)\\s*\\{[^}]*\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return matcher.group();
        }
        return "";
    }


    /**
     * 从getVersion方法提取版本
     *
     * @return {@link String}
     */
    public String getVersionMethodValue() {
        String content = getVersionMethodText();
        Pattern versionPart = Pattern.compile("\"(\\d+\\.\\d+\\.\\d+\\.\\d+)\"");
        Matcher versionMatcher = versionPart.matcher(content);
        if (versionMatcher.find()) {
            return versionMatcher.group(1);
        }
        return "";
    }


    public String getLastAnnotationText() {
        String s = "";
        String content = getAnnotationRangeContent();
        Pattern pattern = Pattern.compile("\\s*[\\*]\\s*[Vv]?\\d+\\.\\d+\\.\\d+\\.\\d+.*?\n", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            s = matcher.group();
        }
        return s;
    }

    public String getLastAnnotationContent() {
        String s = "";
        String content = getAnnotationRangeContent();
        Pattern pattern = Pattern.compile("\\s*[\\*]\\s*([Vv]?\\s*\\d+\\.\\d+\\.\\d+\\.\\d+.*?)\n", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            s = matcher.group(1).trim();
        }
        return s;
    }
}

